﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;
using SMSConsulting.SMSLIB.Utilities;
 
namespace Access.PowerTools.TestConsole
{
    [TestFixture()] 
    public class TestSet_0001_Environment
    {
        [SetUp] 
        public void Setup()
        {
            Access.PowerTools.Settings.Globals.Init();
            Access.PowerTools.Settings.Globals.SimulationModeOn = true;
        }

        [TearDown]
        public void TearDown()
        {
        }

        [Test] 
        public void Test_001_GlobalsInitialization()
        {
            Console.WriteLine(Access.PowerTools.Settings.Globals.MESSAGE_CAPTION);
            Console.WriteLine(Access.PowerTools.Utilities.Globals.MESSAGE_CAPTION);

            bool ret = (string.Compare( 
                  Access.PowerTools.Settings.Globals.MESSAGE_CAPTION, 
                  Access.PowerTools.Utilities.Globals.MESSAGE_CAPTION, true) == 0);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_002_LogFolderExistence()
        {
            string folderPath =
                Access.PowerTools.Settings.Globals.LogFolder;

            bool ret = System.IO.Directory.Exists(folderPath);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }


        [Test]
        public void Test_003_LogFileWriteTest()
        {
            string folderPath =
                Access.PowerTools.Settings.Globals.LogFolder;

            SimpleLogWriter.WriteLogLine("Test at " + DateTime.Now.ToString());

            bool ret = System.IO.File.Exists(Access.PowerTools.Settings.Globals.LogFullPath);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_004_TestFilesFolderExistence()
        {
            string folderPath = Access.PowerTools.Settings.Globals.TestFilesFolder;

            bool ret = System.IO.Directory.Exists(folderPath);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_005_SeedFilesFolderExistence()
        {
            string folderPath = Access.PowerTools.Settings.Globals.SeedFilesFolder;

            bool ret = System.IO.Directory.Exists(folderPath);
     
            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        [Test]
        public void Test_006_NorthdwindDatabaseSeedFilesFolder()
        {
            string folderPath = Access.PowerTools.Settings.Globals.NorthdwindDatabaseSeedFilesFolder;

            bool ret = System.IO.Directory.Exists(folderPath);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

        private bool objectHasData(dao.Database dbs, string objectName)
        {
            bool hasData = false;
            string sql = string.Format("select count(*) from [{0}]", objectName);
            dao.Recordset rst = dbs.OpenRecordset(
                 sql,
                 dao.RecordsetTypeEnum.dbOpenSnapshot,
                 dao.RecordsetOptionEnum.dbForwardOnly,
                 System.Reflection.Missing.Value);
            if (rst != null)
            {
                int count = (int)rst.Fields[0].Value;
                hasData = (count > 0);

                //System.Console.WriteLine("{0} - {1} - {2}", objectName, count, hasData);
                
                rst.Close();
            }
            rst = null;

            return hasData; 
        }

        [Test]
        public void Test_007_DAO()
        {
            string objectName = "Shippers";
            string dbFullPath = Access.PowerTools.Settings.Globals.SeedFilesFolder + "Northwind.mdb";
            dao.DBEngineClass dbEngine = new dao.DBEngineClass();

            bool hasData= false;
            try
            {
                dao.Database dbs = dbEngine.OpenDatabase(dbFullPath, (object)false, (object)false, System.Reflection.Missing.Value);

                hasData = objectHasData(dbs, objectName);

                dbs.Close(); 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);   
                Console.WriteLine(ex.StackTrace);   
            }
            bool ret = (hasData);

            Assert.IsTrue(ret, System.Reflection.MethodInfo.GetCurrentMethod().Name + "() Failed");
        }

    }
}
